/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uit.ieproject.utilites;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import uit.ieproject.crawler.MyCrawler;

/**
 *
 * @author Maivia
 */
public class ImageUtil {

    public static BufferedImage resizeImage(BufferedImage originalImage, int type, int width, int height) {
        BufferedImage resizedImage = new BufferedImage(width, height, type);
        Graphics2D g = resizedImage.createGraphics();
        g.drawImage(originalImage, 0, 0, width, height, null);
        g.dispose();

        return resizedImage;
    }

    public static void showImage(JLabel label, String name, int width, int height, String directory) {
        try {
            if (directory != null) {
                File file = new File(directory);
                if(file.exists()){
                    BufferedImage image = ImageIO.read(file);
                    int type = image.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : image.getType();
                    BufferedImage resizeImage = ImageUtil.resizeImage(image, type, width, height);
                    ImageIcon img = new ImageIcon(resizeImage);
                    label.setIcon(img);
                }
            } else {
                File currentDirectory = FileUtil.getCurrentDir(name, "jpg");
                if (currentDirectory.exists()) {
                    BufferedImage image = ImageIO.read(currentDirectory);
                    int type = image.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : image.getType();
                    BufferedImage resizeImage = ImageUtil.resizeImage(image, type, width, height);
                    ImageIcon img = new ImageIcon(resizeImage);
                    label.setIcon(img);

                } else {
                    BufferedImage image = ImageIO.read(label.getClass().getResource("/uit/ieproject/character/default.jpg"));
                    int type = image.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : image.getType();
                    BufferedImage resizeImage = ImageUtil.resizeImage(image, type, width, height);
                    ImageIcon img = new ImageIcon(resizeImage);
                    label.setIcon(img);
                }
            }
        } catch (Exception ex) {
        }
    }

    public static void saveImageFromWeb(String UrlImage, String path, String filename, String type) throws IOException {
        try {
            BufferedImage imagedownload = ImageIO.read(new URL(UrlImage));
            File currentDir = new File(path + filename + "." + type);
            ImageIO.write(imagedownload, type, currentDir);
        } catch (MalformedURLException ex) {
            Logger.getLogger(MyCrawler.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
